<?php

namespace app\common;

use think\exception\ValidateException;
use think\Validate;
use think\Response;
use think\exception\HttpResponseException;

/**
 * 控制器基础类
 */
abstract class AppController
{
    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     */
    public function __construct()
    {
        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {

    }

    /**
     * 验证数据
     * @access protected
     * @param  array        $data     数据
     * @param  string|array $validate 验证器名或者验证规则数组
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v     = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }

    /**
     * @desc 成功返回
     * @param $data
     * @param int $code
     * @param string $msg
     * @return \think\response\Json
     */
    public function success($data = [], $code = 0, $msg = 'ok')
    {
        $result = [];
        if (is_array($data))
        {
            $arrayKeys = array_keys($data);
            // 如果传了data,就按照原数组处理
            if (in_array('data', $arrayKeys, true))
            {
                $result = $data;
            } else {
                $result['data'] = $data;
            }
        } else {
            $result['data'] = $data;
        }

        $result['code'] = $code;
        $result['msg'] = $msg;

        return json($result);
    }

    /**
     * @desc 失败返回
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return \think\response\Json
     */
    public function error($code = 1, $msg = '请求失败', $data = [])
    {
        $result['code'] = $code;
        $result['msg'] = $msg;
        $result['data'] = $data;
        return json($result);
    }

    /**
     * @desc 是否为ajax请求
     * @return bool
     */
    public function isAjax()
    {
        return request()->isAjax();
    }

    /**
     * @desc 跳转
     * @param $url
     * @param int $code
     * @param array $with
     */
    protected function redirect($url, $code = 302, $with = [])
    {
        $response = Response::create($url, 'redirect');

        $response->code($code)->with($with);

        throw new HttpResponseException($response);
    }
}
